package cn.hchaojie.camp.day12;

import java.io.File;

import org.junit.Test;

public class DemoRegex {
	// 判断文件是否是excel文件
	@Test
	public void test() {
		String fileName = "D:\\camp\\camp2018";
		
		listDir(new File(fileName));
	}
	
	public static void listDir(File dir) {
		File[] listFiles = dir.listFiles();
		
		for (File f : listFiles) {
			if (f.isDirectory()) {
				listDir(f);
			} else {
				if (isExcel2(f.getName())) {
					System.out.println(f.getName());
				}
			}
		}
	}
	
	public static boolean isExcel(String fileName) {
		int c = fileName.lastIndexOf(".");
		String suffix = fileName.substring(c + 1);
		
		if ("docx".equals(suffix)) {
			return true;
		} else {
			return false;
		}
	}
	
	// 正则表达式匹配文件后缀名
	// regex  => regular expression
	public static boolean isExcel2(String fileName) {
		// .*\.(docx)
		return fileName.matches(".*\\.(docx|xlsx)");
	}
	
	@Test
	public void testSplit() {
		String str = "There are many resources online that can help you with the\r\n" + 
				"preparation. For the most part I read articles on system design\r\n" + 
				"interviews, architectures of large-scale systems, and case studies.";
		
		String[] strs = str.split("[ .,\r\n]");
		for (String s : strs) {
			System.out.println(s);
		}
	}
	
	@Test
	public void testReplace() {
		String str = "aabc80bb中文90dddd";
		String str2 = str.replaceAll("[0-9]", "");
		
		System.out.println(str2);
	}
	
	@Test
	public void test1() {
		String regex = "^abc";
		System.out.println("abc".matches(regex));
		System.out.println("ababc".matches(regex));
		System.out.println("abcd".matches(regex));
	}

	@Test
	public void test2() {
		String regex = ".*abc$";
		System.out.println("abc".matches(regex));
		System.out.println("ababc".matches(regex));
		System.out.println("中国abc".matches(regex));
		System.out.println("abcd".matches(regex));
	}
	
	// 表示次数的
	// *   0个以上
	// +   1个以上
	// ?   0个或者1个
	// {n} 出现n次  
	// {n,} 出现n次以上
	// {n,m} 出现n到m次，包括n，m
	@Test
	public void test3() {
//		String regex = "abc*d";
//		System.out.println("abd".matches(regex));
//		System.out.println("abcd".matches(regex));
//		System.out.println("abccd".matches(regex));
//		System.out.println("abcabcd".matches(regex));

//		String regex = "(abc)*d";
//		System.out.println("d".matches(regex));
//		System.out.println("abd".matches(regex));
//		System.out.println("abcd".matches(regex));
//		System.out.println("abccd".matches(regex));
//		System.out.println("abcabcd".matches(regex));
		
		String regex = "abc{2,4}d";
		System.out.println("abd".matches(regex));
		System.out.println("abcd".matches(regex));
		System.out.println("abccd".matches(regex));
		System.out.println("abcccd".matches(regex));
		System.out.println("abccccd".matches(regex));
		System.out.println("abcccccd".matches(regex));
	}
	
	@Test
	public void test4() {
		// .+\.(png|gif|jpg|jpeg)$
//		String regex = ".+\\.(png|gif|jpg|jpeg)$";
//		
//		String regex2 = "[abcd]";
//		String regex3 = "(a|bcd|c|d)";
//		System.out.println("a".matches(regex3));
//		System.out.println("a".matches(regex2));
//		
//		String regex4 = "[a-zA-Z0-9]+";
//		System.out.println("a".matches(regex4));
//		System.out.println("A".matches(regex4));
//		System.out.println("0".matches(regex4));
//		System.out.println("9".matches(regex4));
//		System.out.println("中".matches(regex4));
//		System.out.println("-".matches(regex4));
//
//		System.out.println("aaaAAA889".matches(regex4));
		
		// \d [0-9]		digit
		// \w [a-zA-Z0-9_]
		System.out.println("99992".matches("\\d+"));
		System.out.println("aaaAAA000999_".matches("\\w+"));
		System.out.println("aaaAAA000999_-".matches("\\w+"));

		
//		System.out.println("a.jpg".matches(regex));
//		System.out.println(".jpg".matches(regex));
//		System.out.println("a.jp".matches(regex));
	}
	
	@Test
	public void test5() {
		// 密码必须是6-18位 \w{6, 18}
//		String regex = "\\w{6,18}";
//		System.out.println("123456".matches(regex));
//		System.out.println("1234".matches(regex));
//		System.out.println("passw0rd".matches(regex));
//		System.out.println("ab$d".matches(regex));
//		System.out.println("abcdabcdabcdabcdabcd".matches(regex));
//		System.out.println("abcdefg".matches(regex));
		
		// 密码是6-18位，必须含有数字
//		String regex2 = "\\w{6,18}";
		
		// 必须含数字	\d+
//		String regex3 = ".*\\d+.*";
//		System.out.println("a3c".matches(regex3));
		
		// 断言  (?=.*\d+.*)\w{6,18}
//		String regex4 = "(?=.*\\d+)\\w{6,18}";
//		System.out.println("abcd1234".matches(regex4));
//		System.out.println("abcdefgh".matches(regex4));
//		System.out.println("abcdefg3".matches(regex4));
		
		// 密码是6-18位，必须含有数字，必须是大写字母开头
		// ^[A-Z]
		// (?=)(?=)
//		String regex5 = "(?=.*\\d+)(?=^[A-Z])\\w{6,18}";
//		System.out.println("Abcd1234".matches(regex5));
//		System.out.println("Abcdefgh".matches(regex5));
//		System.out.println("Abcdefg3".matches(regex5));
		
		// ^[A-Z]\w{5,17}
		String regex5 = "(?=.*\\d+)^[A-Z]\\w{5,17}";
		System.out.println("Abcd1234".matches(regex5));
		System.out.println("Abcdefgh".matches(regex5));
		System.out.println("Abcdefg3".matches(regex5));
	}
	
	@Test
	public void test6() {
		// 非负整数  
		// 0, 1, 3, 11, ...
		// 0
		// 1, 10

		// 
		// [0-9]|[1-9][0-9]+
//		String reg1 = "[1-9][0-9]*|0";
//		String reg2 = "[0-9]|[1-9][0-9]+";
//		
//		System.out.println("123".matches(reg1));
//		System.out.println("0123".matches(reg1));
//		System.out.println("123".matches(reg2));
//		System.out.println("0123".matches(reg2));
		
		
		String reg1 = "[\\+\\-]?[1-9]\\d*|0";
		System.out.println("+123".matches(reg1));
		System.out.println("123".matches(reg1));
		System.out.println("+-123".matches(reg1));
	}
	
	@Test
	public void test7() {
		// 20-99的数字
//		String regex = "[20-99]";	// [0-9]
//		String regex = "(20-99)";	// [0-9]
		String regex = "[2-9][0-9]|100";	// [0-9]
		System.out.println("20".matches(regex));
		System.out.println("99".matches(regex));
		System.out.println("9".matches(regex));
		System.out.println("100".matches(regex));
		
		// 20-199  20-99 100-199
	}
	
		
		
	@Test
	public void test8() {
		// ((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\d{8} 
		String regex = "((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}";
		System.out.println("13812341234".matches(regex));
		System.out.println("1381234123".matches(regex));
		System.out.println("19812341234".matches(regex));
	}
	
	
	@Test
	public void testURL() {
		// /^((ht|f)tps?):\/\/[\w\-]+(\.[\w\-]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?$/;
		String s = "^((ht|f)tps?):\\/\\/[\\w\\-]+(\\.[\\w\\-]+)+([\\w\\-"
				+ "\\.,@?^=%&:\\/~\\+#]*[\\w\\-\\@?^=%&\\/~\\+#])?$";
		
		System.out.println("http://baidu.com".matches(s));
		System.out.println("http://baidu".matches(s));
		System.out.println("mailto://baidu.com".matches(s));

	}

	@Test
	public void testEmail() {
		// [\w[\.\-]]+@[\w[\.\-]]+\.([A-Za-z]{2,4})
		String regex = "[\\w[\\.\\-]]+@[\\w[\\.\\-]]+\\.([A-Za-z]{2,4})";
		
		System.out.println("qq@bb.com".matches(regex));
	}
}
